package com.ziyun.job.modules.job.task.finance;

import com.baomidou.mybatisplus.plugins.Page;
import com.cloud.bus.core.EventBusPusher;
import com.cloud.bus.event.order.OrderCancelBatchEvent;
import com.ziyun.erp.modules.order.entity.OrderInfoEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author guizhao
 * @Description: 财务自动确认收款
 * @date 2018-11-28 14:16
 */
@Slf4j
@Component("orderPayConfirmTask")
public class OrderPayConfirmTask {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ErpOrderService erpOrderService;

    /**
     * 财务自动确认付款（支付宝、微信）
     */
    public void orderPayConfirmTask(){
        try {

            // 当前页数
            int currentPage = 1;

            Page<OrderInfoEntity> page = new Page<>(currentPage,50);
            // 查询 N 天内未支付订单
            Page<OrderInfoEntity> list = orderInfoService.selecOrderPayConfirmPage(page);

            // 总页数
            int totalPages = list.getPages();
            do {
                log.info("财务自动确认付款（支付宝、微信）---订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+currentPage);
                if(list != null && list.getTotal() > 0){
                    orderInfoService.confirmOrderPayTask(list.getRecords());
                }
                currentPage++;
                if(currentPage <= totalPages){
                    page = new Page<>(1,50);
                    list = orderInfoService.selecOrderPayConfirmPage(page);
                }
                Thread.sleep(100);
            }while (currentPage < totalPages);
            log.info("财务自动确认付款（支付宝、微信）完成");
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

}
